////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///  Table 1: Model Estimates

/// this populates \omega and B values and tests dfference between groups
use "$data/eoydata_late.dta", clear

gen lowgroup = 1- highgroup
gen xxdiff = guess - value
gen xxdiffv_l = (mean_theory - value)*lowgroup
gen xxdiffv_h = (mean_theory - value)*highgroup

tab alpha_opt treatmentname if highgroup ==0
tab alpha_opt treatmentname if highgroup ==1

tab var_signal treatmentname if highgroup ==0
tab var_signal treatmentname if highgroup ==1

reg guess highgroup if  treatmentname =="Baseline" & filteredsample ==1
reg value highgroup if  treatmentname =="Baseline" & filteredsample ==1
test highgroup ==20

reg xxdiff xxdiffv_l xxdiffv_h lowgroup highgroup if treatmentname =="Baseline" & filteredsample ==1, nocons cluster(id)
test lowgroup ==highgroup
test xxdiffv_l ==xxdiffv_h
reg xxdiff xxdiffv_l xxdiffv_h lowgroup highgroup if treatmentname =="NoGroup" & filteredsample ==1, nocons cluster(id)
test lowgroup ==highgroup
test xxdiffv_l ==xxdiffv_h
reg xxdiff xxdiffv_l xxdiffv_h lowgroup highgroup if treatmentname =="SignalFirst" & filteredsample ==1, nocons cluster(id)
test lowgroup ==highgroup
test xxdiffv_l ==xxdiffv_h
reg xxdiff xxdiffv_l xxdiffv_h lowgroup highgroup if treatmentname =="OneGroup" & filteredsample ==1, nocons cluster(id)
test lowgroup ==highgroup
test xxdiffv_l ==xxdiffv_h



/// replicates other values reported 
use "$data/eoydata_late.dta", clear
drop if filteredsample ==0


preserve
// This is for Delta_g values
gen foo = bias/(1-alpha)
egen vs_x = mean(foo), by(highgroup treatment)
tab vs_x highgroup if treatmentname =="Baseline"
tab vs_x highgroup if treatmentname =="NoGroup"
tab vs_x highgroup if treatmentname =="SignalFirst"
tab vs_x highgroup if treatmentname =="OneGroup"


restore
preserve
// This is for variance and omega bay values
replace signal = (guess - bias - alpha*mean_theory) / (1- alpha)
gen sigerror = signal - value
gen foo = sigerror^2
egen vs_x = mean(foo), by(highgroup treatment)
gen omega_bay = (1/var_theory)/ ((1/vs_x) + (1/var_theory)) 
tab vs_x highgroup if treatmentname =="Baseline"
tab vs_x highgroup if treatmentname =="NoGroup"
tab vs_x highgroup if treatmentname =="SignalFirst"
tab vs_x highgroup if treatmentname =="OneGroup"

tab omega_bay highgroup if treatmentname =="Baseline"
tab omega_bay highgroup if treatmentname =="NoGroup"
tab omega_bay highgroup if treatmentname =="SignalFirst"
tab omega_bay highgroup if treatmentname =="OneGroup"


/// statistical tests using bootstrapping 



/// replicates other values reported 
use "$data/eoydata_late.dta", clear
drop if filteredsample ==0
tab treatment treatmentname 
//1. bl, 2. sf, 3.ng, 4. og
drop if filteredsample ==0
gen foo = 1 
egen tfoo = sum(foo), by(treatment)
drop foo
gen tnobs = tfoo/38
*gen tnobs = tfoo/75

gen delta = bias/(1- alpha)
replace diffv_h_bl = (mean_theory - value)highgroup
replace diffv_l_bl = (mean_theory - value)*(1-highgroup)
replace diff = (guess - value)
replace d_bl = 1


set seed 666
forvalues i=1/500{
preserve
bsample tnobs, strata(treatment) cluster(id)
forvalues j = 1/4{
reg diff diffv_h_bl diffv_l_bl d_bl highgroup if treatment == `j', nocons cluster(id)
matrix b = e(b)
replace alpha = b[1,1] if highgroup ==1 & treatment == `j'
replace alpha = b[1,2] if highgroup ==0 & treatment == `j'
replace bias = b[1,3] + b[1,4] if highgroup ==1 & treatment == `j'
replace bias = b[1,3] if highgroup ==0 & treatment == `j'
}
replace signal = (guess - bias - alpha*mean_theory) / (1- alpha)
gen sigerror = signal - value


// This is for variance 
*gen foo = (sigerror)^2
*egen vs_x = mean(foo), by(highgroup treatment)

// This is for the Delta_g 
*gen foo = bias/(1-alpha)
*egen vs_x = mean(foo), by(highgroup treatment)

// This is for the alpha opt - alpha
gen foo = (sigerror)^2
egen vs_x = mean(foo), by(highgroup treatment)
replace vs_x = (1/var_theory)/ ((1/vs_x) + (1/var_theory)) - alpha

// This is for alpha
*gen vs_x =  alpha

gen foo1_h = vs_x if treatment ==1 & highgroup ==1
gen foo2_h = vs_x if treatment ==2 & highgroup ==1
gen foo3_h = vs_x if treatment ==3 & highgroup ==1
gen foo4_h = vs_x if treatment ==4 & highgroup ==1
gen foo1_l = vs_x if treatment ==1 & highgroup ==0
gen foo2_l = vs_x if treatment ==2 & highgroup ==0
gen foo3_l = vs_x if treatment ==3 & highgroup ==0
gen foo4_l = vs_x if treatment ==4 & highgroup ==0
egen gdh1_x = max(foo1_h)
egen gdh2_x = max(foo2_h)
egen gdh3_x = max(foo3_h)
egen gdh4_x = max(foo4_h)
egen gdl1_x = max(foo1_l)
egen gdl2_x = max(foo2_l)
egen gdl3_x = max(foo3_l)
egen gdl4_x = max(foo4_l)
local xxxh1 = gdh1_x
local xxxh2 = gdh2_x
local xxxh3 = gdh3_x
local xxxh4 = gdh4_x
local xxxl1 = gdl1_x
local xxxl2 = gdl2_x
local xxxl3 = gdl3_x
local xxxl4 = gdl4_x
restore
gen gd1h_`i' = `xxxh1' 
gen gd2h_`i' = `xxxh2' 
gen gd3h_`i' = `xxxh3' 
gen gd4h_`i' = `xxxh4' 
gen gd1l_`i' = `xxxl1' 
gen gd2l_`i' = `xxxl2' 
gen gd3l_`i' = `xxxl3' 
gen gd4l_`i' = `xxxl4' 
}


// for alpha
*gen diff_orig =alpha
// for alpha_opt vs alpha
*gen diff_orig = alpha_opt - alpha
// for signal variance
*gen diff_orig = var_signal
// for delta_g
gen diff_orig =  bias/(1- alpha)


collapse gd*  alpha alpha_opt bias var_signal diff_orig, by(treatment treatmentname highgroup)

reshape long gd1h_ gd2h_ gd3h_ gd4h_ gd1l_ gd2l_ gd3l_ gd4l_ , i(treatment treatmentname highgroup) j(index)  


rename gd1h_ gd1h
rename gd2h_ gd2h
rename gd3h_ gd3h
rename gd4h_ gd4h
rename gd1l_ gd1l
rename gd2l_ gd2l
rename gd3l_ gd3l
rename gd4l_ gd4l

gen var_signal_boot = 999
replace var_signal_boot = gd1h if treatment ==1 & highgroup ==1
replace var_signal_boot = gd2h if treatment ==2 & highgroup ==1
replace var_signal_boot = gd3h if treatment ==3 & highgroup ==1
replace var_signal_boot = gd4h if treatment ==4 & highgroup ==1
replace var_signal_boot = gd1l if treatment ==1 & highgroup ==0
replace var_signal_boot = gd2l if treatment ==2 & highgroup ==0
replace var_signal_boot = gd3l if treatment ==3 & highgroup ==0
replace var_signal_boot = gd4l if treatment ==4 & highgroup ==0


tab var_signal treatmentname if highgroup ==1
tab var_signal treatmentname if highgroup ==0

gen diff = var_signal_boot
gen lowgroup = 1-highgroup


bysort treatmentname highgroup: summarize diff, detail

sort treatmentname highgroup
by treatmentname highgroup: egen ub = pctile(diff), p(97.5)
by treatmentname highgroup: egen lb = pctile(diff), p(2.5)


// p-values for whether values are different from zero
preserve
replace diff = diff - diff_orig
gen dummy = 0
replace dummy = 1 if diff< -abs(diff_orig) |  diff> abs(diff_orig)
egen pvalue = mean(dummy), by(treatmentname highgroup)
by treatmentname highgroup: summarize pvalue
restore

// p-values for whether values are different between high vand low groups
preserve
gen foo = diff
replace foo = . if lowgroup ==1
egen diffhigh = max(diff), by(treatmentname index)
drop foo
gen foo = diff_orig
replace foo = . if lowgroup ==1
egen diffhigh_orig = max(diff_orig), by(treatmentname)
drop if highgroup ==1
gen diffnew = diffhigh - diff
gen diffnew_orig = diffhigh_orig - diff_orig
replace diffnew = diffnew - diffnew_orig
gen dummy = 0
replace dummy = 1 if diffnew< -abs(diffnew_orig) |  diffnew> abs(diffnew_orig)
egen pvalue = mean(dummy), by(treatmentname)
by treatmentname: summarize pvalue
restore

// p-values for whether values are different between treatments
//1. bl, 2. sf, 3.ng, 4. og
preserve
gen foo = diff
replace foo = . if treatment >1
egen diffbl = max(diff), by(highgroup index)
drop foo
gen foo = diff_orig
replace foo = . if treatment >1
egen diffbl_orig = max(diff_orig), by(highgroup)
drop if treatmentname =="Baseline"
gen diffnew = diffbl - diff
gen diffnew_orig = diffbl_orig - diff_orig
replace diffnew = diffnew - diffnew_orig
gen dummy = 0
replace dummy = 1 if diffnew< -abs(diffnew_orig) |  diffnew> abs(diffnew_orig)
egen pvalue = mean(dummy), by(treatmentname highgroup)
by treatmentname highgroup: summarize pvalue
restore
 

	
	
	
	
	

